/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.service.impl;

import com.minierp.bean.CooperBillBean;
import com.minierp.bean.CooperBillItemBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.AccessoryDao;
import com.minierp.dao.AdjustBillDao;
import com.minierp.dao.BankAccountBalanceDao;
import com.minierp.dao.BankAccountDetailDao;
import com.minierp.dao.BusinessBillDao;
import com.minierp.dao.BusinessBillItemDao;
import com.minierp.dao.CooperBillDao;
import com.minierp.dao.CooperBillItemDao;
import com.minierp.dao.FinanceDetailDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.PayBillDao;
import com.minierp.dao.SerialNumberDao;
import com.minierp.err.DataChangedException;
import com.minierp.err.NotAllowException;
import com.minierp.helper.PageHelper;
import com.minierp.model.BusinessBill;
import com.minierp.model.BusinessBillExt;
import com.minierp.model.BusinessBillItem;
import com.minierp.model.BusinessBillItemExt;
import com.minierp.model.BusinessProfit;
import com.minierp.model.CodeRule;
import com.minierp.model.CooperBill;
import com.minierp.model.CooperBillExt;
import com.minierp.model.CooperBillItem;
import com.minierp.model.EnterpriseInfo;
import com.minierp.model.Establishment;
import com.minierp.model.FinanceDetail;
import com.minierp.model.SystemLog;
import com.minierp.service.CooperBillService;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class CooperBillServiceImpl implements CooperBillService {
	public void txInsert(CooperBillBean form, String exam, EnterpriseInfo ent,
			UserBean ub, CooperBillDao cooperBillDao,
			CooperBillItemDao cooperBillItemDao,
			SerialNumberDao serialNumberDao, AccessoryDao accessoryDao,
			BankAccountBalanceDao bankAccountBalanceDao,
			BusinessBillDao businessBillDao,
			BusinessBillItemDao businessBillItemDao, IdGenerator idGenerator)
			throws Exception {
		CooperBill obj = form.getCooperBill();
		obj.setCreateTime(new Date());
		obj.setId(idGenerator.getId(CooperBill.class.getName()));
		if (!form.getCodeRule().getGenerateRule().equals("MANUAL")) {
			obj.setBillCode(PageHelper.generateCode(serialNumberDao, form
					.getCodeRule(), "", form.getCooperBill().getTallySetId()));

			for (CooperBill be = cooperBillDao.get(obj.getBillCode()); be != null; be = cooperBillDao
					.get(obj.getBillCode())) {
				obj.setBillCode(PageHelper.generateCode(serialNumberDao, form
						.getCodeRule(), "", form.getCooperBill()
						.getTallySetId()));
			}
		}

		this.setCost(form, ent.getBusiLengType());
		cooperBillDao.save(obj);
		CooperBillExt be1 = new CooperBillExt();
		be1.setId(obj.getId());
		if (exam != null && exam.equals("1")) {
			be1.setExamined("Y");
			be1.setPaiedCost(obj.getImprest());
			if (obj.getCost().compareTo(be1.getPaiedCost()) == 0) {
				be1.setPaied("Y");
			}

			be1.setExamineTime(new Date());
			be1.setExaminerId(ub.getUser().getStafferId());
			be1.setExaminerName(ub.getStaffer().getStafferName());
		}

		cooperBillDao.save(be1);
		this.saveItems(obj.getId(), "N", ent.getBusiCooComp(),
				be1.getExamined(), form.getItems(), ub, cooperBillItemDao,
				businessBillDao, businessBillItemDao, idGenerator);
		accessoryDao.update(form.getTmpCode(), obj.getId());
		if (be1.getExamined().equals("Y")
				&& obj.getImprest().compareTo(new BigDecimal(0)) != 0) {
			String est = form.getCooperatorName() + "," + obj.getMark();
			if (obj.getContent() != null && !obj.getContent().equals("")) {
				est = est + "," + obj.getContent();
			}

			PageHelper.updateBankAccount(obj.getBankAccountId(),
					obj.getTallySetId(), obj.getId(), obj.getImprest(),
					obj.getCooperDate(), be1.getExamineTime(),
					obj.getBillCode(), "COOPER_BILL", "", "外协预付", "P",
					obj.getManagerName(), est, "COOPER", "SAVE_EXAM", false,
					(BankAccountDetailDao) null, bankAccountBalanceDao,
					idGenerator);
		}

		if (be1.getExamined().equals("Y")) {
			Establishment est1 = (Establishment) cooperBillDao.get(
					Establishment.class, obj.getCooperatorId());
			if (est1 != null
					&& (est1.getBusTime() == null || est1.getBusTime().before(
							obj.getCooperDate()))) {
				est1.setBusTime(obj.getCooperDate());
				cooperBillDao.update(est1);
			}
		}

		if (be1.getExamined().equals("Y")) {
			this.insertFinanceDetail(obj, cooperBillDao, idGenerator);
		}

	}

	private void setCost(CooperBillBean form, String busiLengType) {
		List items = form.getItems();
		BigDecimal cost = new BigDecimal(0);
		BigDecimal amount = new BigDecimal(0);
		BigDecimal square = new BigDecimal(0);
		BigDecimal lengM = new BigDecimal(0);
		BigDecimal lengCm = new BigDecimal(0);

		for (int i = 0; i < items.size(); ++i) {
			CooperBillItemBean bean = (CooperBillItemBean) items.get(i);
			CooperBillItem item = bean.getCooperBillItem();
			if (!bean.getStatus().equals("DELETE")) {
				BigDecimal squa;
				if (item.getPriceType().equals("AREA")) {
					squa = item.getLength().multiply(item.getWidth())
							.multiply(item.getAmount());
					if (item.getLengType().equals("MM")) {
						squa = squa.divide(new BigDecimal(1000000), 3, 4);
					} else {
						squa = squa.setScale(3, 4);
					}

					item.setSquareNum(squa);
					item.setCost(item.getUnitPrice().multiply(squa)
							.setScale(2, 4));
					square = square.add(squa);
				} else if (item.getPriceType().equals("AMOUNT")) {
					item.setCost(item.getUnitPrice().multiply(item.getAmount())
							.setScale(2, 4));
					amount = amount.add(item.getAmount());
				} else if (item.getPriceType().equals("LENGTH")) {
					if (busiLengType.equals("WIDTH")) {
						squa = item.getAmount().multiply(item.getLength());
					} else if (busiLengType.equals("TALL")) {
						squa = item.getAmount().multiply(item.getWidth());
					} else if (item.getLength().compareTo(item.getWidth()) > 0) {
						squa = item.getAmount().multiply(item.getLength());
					} else {
						squa = item.getAmount().multiply(item.getWidth());
					}

					squa = squa.setScale(2, 4);
					item.setSquareNum(squa);
					item.setCost(item.getUnitPrice().multiply(squa)
							.setScale(2, 4));
					if (item.getLengType().equals("M")) {
						lengM = lengM.add(squa);
					} else {
						lengCm = lengCm.add(squa);
					}
				}

				cost = cost.add(item.getCost());
			}
		}

		cost = cost.subtract(form.getCooperBill().getFavCost());
		form.getCooperBill().setCost(cost);
		form.getCooperBill().setAmount(amount);
		form.getCooperBill().setSquare(square);
		form.getCooperBill().setLengCm(lengCm);
		form.getCooperBill().setLengM(lengM);
	}

	public void txInsertForBegiImpo(CooperBill obj, CodeRule cr,
			CooperBillItem item, EnterpriseInfo ent, UserBean ub,
			CooperBillDao cooperBillDao, SerialNumberDao serialNumberDao,
			IdGenerator idGenerator) throws Exception {
		obj.setCreateTime(new Date());
		obj.setId(idGenerator.getId(CooperBill.class.getName()));
		if (cr.getGenerateRule().equals("MANUAL")) {
			throw new NotAllowException("外协单编号规则必须设置成自动产生！");
		} else {
			obj.setBillCode(PageHelper.generateCode(serialNumberDao, cr, "",
					obj.getTallySetId()));

			for (CooperBill be = cooperBillDao.get(obj.getBillCode()); be != null; be = cooperBillDao
					.get(obj.getBillCode())) {
				obj.setBillCode(PageHelper.generateCode(serialNumberDao, cr,
						"", obj.getTallySetId()));
			}

			cooperBillDao.save(obj);
			CooperBillExt be1 = new CooperBillExt();
			be1.setId(obj.getId());
			be1.setExamined("Y");
			be1.setExamineTime(new Date());
			be1.setExaminerId(ub.getUser().getStafferId());
			be1.setExaminerName(ub.getStaffer().getStafferName());
			be1.setGathered("Y");
			cooperBillDao.save(be1);
			item.setId(idGenerator.getId(CooperBillItem.class.getName()));
			item.setBillId(obj.getId());
			cooperBillDao.save(item);
			Establishment est = (Establishment) cooperBillDao.get(
					Establishment.class, obj.getCooperatorId());
			if (est != null
					&& (est.getBusTime() == null || est.getBusTime().before(
							obj.getCooperDate()))) {
				est.setBusTime(obj.getCooperDate());
				cooperBillDao.update(est);
			}

			this.insertFinanceDetail(obj, cooperBillDao, idGenerator);
		}
	}

	private void insertFinanceDetail(CooperBill obj,
			CooperBillDao cooperBillDao, IdGenerator idGenerator) {
		FinanceDetail fd = new FinanceDetail();
		fd.setId(idGenerator.getId(FinanceDetail.class.getName()));
		fd.setBillDate(obj.getCooperDate());
		fd.setBillId(obj.getId());
		fd.setBillType("COOPER_BILL");
		fd.setRemCost(obj.getCost().subtract(obj.getImprest()));
		fd.setConCost(obj.getCost());
		fd.setRpCost(obj.getImprest());
		fd.setEstId(obj.getCooperatorId());
		fd.setFinanceType("P");
		fd.setTallySetId(obj.getTallySetId());
		cooperBillDao.save(fd);
	}

	public void txUpdate(CooperBillBean form, EnterpriseInfo ent,
			String editType, CooperBillDao cooperBillDao,
			CooperBillItemDao cooperBillItemDao, PayBillDao payBillDao,
			AdjustBillDao adjustBillDao,
			BankAccountBalanceDao bankAccountBalanceDao,
			BankAccountDetailDao bankAccountDetailDao,
			BusinessBillDao businessBillDao,
			BusinessBillItemDao businessBillItemDao, IdGenerator idGenerator,
			UserBean ub, FinanceDetailDao financeDetailDao) throws Exception {
		CooperBill obj = form.getCooperBill();
		Object[] sv = cooperBillDao.getStatusValues(obj.getId());
		if (sv == null) {
			throw new Exception("Can\'t get status values for cooper bill:"
					+ obj.getId());
		} else {
			long existSycVal = ((Date) sv[0]).getTime();
			long oldEstId = Long.parseLong(sv[4].toString());
			CooperBillExt be = (CooperBillExt) businessBillDao.get(
					CooperBillExt.class, obj.getId());
			if (existSycVal != form.getSychronizeValue()) {
				throw new DataChangedException("外协单已被其他操作修改，请关闭页面再打开后重新操作！");
			} else if (be.getEnabled().equals("N")) {
				throw new NotAllowException("外协单已作废，不能修改！");
			} else if ((editType == null || editType.equals(""))
					&& be.getExamined().equals("Y")) {
				throw new NotAllowException("外协单已审核，不能修改！");
			} else if (ent.getCoopModi().equals("N")
					&& be.getHasPay().equals("Y")) {
				throw new NotAllowException("外协单已付款，不能修改！");
			} else if (be.getExamined().equals("Y")
					&& oldEstId != form.getCooperBill().getCooperatorId()) {
				throw new NotAllowException("外协单已审核，不能修改外协单位！");
			} else {
				this.setCost(form, ent.getBusiLengType());
				obj.setModifierId(ub.getStaffer().getId());
				obj.setModifierName(ub.getStaffer().getStafferName());
				obj.setModifyTime(new Date());
				obj.setUpdateTime(new Date());
				cooperBillDao.update(obj);
				if (be.getExamined().equals("Y")) {
					be.setPaiedCost(be.getPayCost().add(obj.getImprest()));
					if (obj.getCost().subtract(be.getPaiedCost())
							.add(be.getAdjustCost())
							.compareTo(new BigDecimal(0)) == 0) {
						be.setPaied("Y");
					} else {
						be.setPaied("N");
					}
				}

				if (be.getExamined().equals("Y")) {
					String gathered = "Y";

					for (int j = 0; j < form.getItems().size(); ++j) {
						CooperBillItemBean bean = (CooperBillItemBean) form
								.getItems().get(j);
						if (!bean.getStatus().equals("DELETE")) {
							if (bean.getCooperBillItem().getGatheredAmount()
									.compareTo(new BigDecimal(0)) == 0) {
								gathered = "N";
							} else if (bean
									.getCooperBillItem()
									.getAmount()
									.compareTo(
											bean.getCooperBillItem()
													.getGatheredAmount()) == 1) {
								gathered = "P";
								break;
							}
						}
					}

					be.setGathered(gathered);
				}

				cooperBillDao.update(be);
				this.saveItems(obj.getId(), be.getHasGather(),
						ent.getBusiCooComp(), be.getExamined(),
						form.getItems(), ub, cooperBillItemDao,
						businessBillDao, businessBillItemDao, idGenerator);
				if (be.getExamined().equals("Y")) {
					financeDetailDao.delete(obj.getId(), "COOPER_BILL");
					this.insertFinanceDetail(obj, cooperBillDao, idGenerator);
				}

			}
		}
	}

	private void saveItems(long billId, String hasGath, String busiCooComp,
			String examined, List<?> items, UserBean ub,
			CooperBillItemDao cooperBillItemDao,
			BusinessBillDao businessBillDao,
			BusinessBillItemDao businessBillItemDao, IdGenerator idGenerator)
			throws NotAllowException {
		int newNum = 0;

		for (int curItemId = 0; curItemId < items.size(); ++curItemId) {
			CooperBillItemBean bean = (CooperBillItemBean) items.get(curItemId);
			if (bean.getStatus().equals("NEW")) {
				++newNum;
			}
		}

		long arg31 = 0L;
		if (newNum > 0) {
			arg31 = idGenerator.getId(CooperBillItem.class.getName(), newNum);
		}

		int areaIndex;
		String itemName;
		for (areaIndex = 0; areaIndex < items.size(); ++areaIndex) {
			CooperBillItemBean lengthIndex = (CooperBillItemBean) items
					.get(areaIndex);
			lengthIndex.getCooperBillItem().setBillId(billId);
			long amountIndex = lengthIndex.getCooperBillItem().getBbItemId();
			long bean1 = lengthIndex.getOldBbItemId();
			String oldBbCode = lengthIndex.getOldBbCode();
			itemName = lengthIndex.getCooperBillItem().getBbCode();
			if (oldBbCode == null) {
				oldBbCode = "";
			}

			if (itemName == null) {
				itemName = "";
			}

			if (lengthIndex.getStatus().equals("DELETE")) {
				if (lengthIndex.getOldId() != 0L) {
					if (hasGath.equals("Y")) {
						throw new NotAllowException("外协单已经收货，不能删除明细项目！");
					}

					cooperBillItemDao.delete(lengthIndex.getOldId());
				}
			} else if (lengthIndex.getStatus().equals("EDIT")) {
				cooperBillItemDao.update(lengthIndex.getCooperBillItem());
			} else if (lengthIndex.getStatus().equals("NEW")) {
				lengthIndex.getCooperBillItem().setId(arg31);
				cooperBillItemDao.save(lengthIndex.getCooperBillItem());
				++arg31;
			}

			BusinessBillItemExt amount = null;
			BusinessBillItemExt bbItem = null;
			BusinessBillItem bbIe = null;
			BusinessBillItem tmpBean = null;
			String bb;
			BigDecimal p;
			String be;
			BusinessBillExt cooperCost;
			if (amountIndex != 0L) {
				bbIe = (BusinessBillItem) cooperBillItemDao.get(
						BusinessBillItem.class, amountIndex);
				amount = (BusinessBillItemExt) cooperBillItemDao.get(
						BusinessBillItemExt.class, amountIndex);
				if (busiCooComp != null && busiCooComp.equals("Y")) {
					if (amount != null) {
						bb = amount.getCoopComp();
						p = cooperBillItemDao.getCooperedAmount(amountIndex);
						amount.setCoopAmou(p);
						if (p.compareTo(bbIe.getAmount()) >= 0) {
							be = "Y";
						} else {
							be = "N";
						}

						if (!be.equals(bb)) {
							amount.setCoopComp(be);
							amount.setOperTime(new Date());
							amount.setOperId(ub.getUser().getStafferId());
							amount.setOperName(ub.getStaffer().getStafferName());
							cooperBillItemDao.update(amount);
							cooperCost = (BusinessBillExt) cooperBillItemDao
									.get(BusinessBillExt.class,
											bbIe.getBillId());
							if (cooperCost != null) {
								if (businessBillItemDao.isAllCompleted(bbIe
										.getBillId())) {
									cooperCost.setCompleted("Y");
									cooperCost.setCompleteTime(new Date());
								} else {
									cooperCost.setCompleted("N");
									cooperCost.setCompleteTime((Date) null);
								}

								cooperBillItemDao.update(cooperCost);
							}
						}
					} else if (!lengthIndex.getStatus().equals("DELETE")) {
						throw new NotAllowException("对应的业务单项目已经不存在，业务单号："
								+ lengthIndex.getCooperBillItem().getBbCode());
					}
				}
			}

			if (bean1 != 0L && bean1 != amountIndex) {
				tmpBean = (BusinessBillItem) cooperBillItemDao.get(
						BusinessBillItem.class, bean1);
				bbItem = (BusinessBillItemExt) cooperBillItemDao.get(
						BusinessBillItemExt.class, bean1);
				if (busiCooComp != null && busiCooComp.equals("Y")
						&& bbItem != null) {
					bb = bbItem.getCoopComp();
					p = cooperBillItemDao.getCooperedAmount(bean1);
					bbItem.setCoopAmou(p);
					if (p.compareTo(tmpBean.getAmount()) >= 0) {
						be = "Y";
					} else {
						be = "N";
					}

					if (!be.equals(bb)) {
						bbItem.setCoopComp(be);
						bbItem.setOperTime(new Date());
						bbItem.setOperId(ub.getUser().getStafferId());
						bbItem.setOperName(ub.getStaffer().getStafferName());
						cooperBillItemDao.update(bbItem);
						cooperCost = (BusinessBillExt) cooperBillItemDao.get(
								BusinessBillExt.class, tmpBean.getBillId());
						if (cooperCost != null) {
							if (businessBillItemDao.isAllCompleted(tmpBean
									.getBillId())) {
								cooperCost.setCompleted("Y");
								cooperCost.setCompleteTime(new Date());
							} else {
								cooperCost.setCompleted("N");
								cooperCost.setCompleteTime((Date) null);
							}

							cooperBillItemDao.update(cooperCost);
						}
					}
				}
			}

			if (examined.equals("Y")) {
				BigDecimal profitCost;
				BusinessBill arg41;
				BusinessProfit arg42;
				BigDecimal arg43;
				BusinessBillExt arg44;
				if (!itemName.equals("")) {
					arg41 = businessBillDao.get(itemName);
					if (arg41 != null) {
						arg42 = businessBillDao.getBusinessProfit(arg41
								.getBillCode());
						if (arg42 != null) {
							arg44 = (BusinessBillExt) businessBillDao.get(
									BusinessBillExt.class, arg41.getId());
							arg43 = cooperBillItemDao.getCost(arg41.getId());
							profitCost = arg41.getCost()
									.add(arg44.getAdjustCost()).subtract(arg43)
									.subtract(arg42.getFeeCost())
									.subtract(arg42.getMaterialCost());
							arg42.setCooperCost(arg43);
							arg42.setProfitCost(profitCost);
							businessBillDao.update(arg42);
						}
					}
				}

				if (!itemName.equals(oldBbCode)) {
					arg41 = businessBillDao.get(oldBbCode);
					if (arg41 != null) {
						arg42 = businessBillDao.getBusinessProfit(arg41
								.getBillCode());
						if (arg42 != null) {
							arg44 = (BusinessBillExt) businessBillDao.get(
									BusinessBillExt.class, arg41.getId());
							arg43 = cooperBillItemDao.getCost(arg41.getId());
							profitCost = arg41.getCost()
									.add(arg44.getAdjustCost()).subtract(arg43)
									.subtract(arg42.getFeeCost())
									.subtract(arg42.getMaterialCost());
							arg42.setCooperCost(arg43);
							arg42.setProfitCost(profitCost);
							businessBillDao.update(arg42);
						}
					}
				}
			}
		}

		if (busiCooComp != null && busiCooComp.equals("Y")) {
			areaIndex = 0;
			int arg32 = 0;
			int arg33 = 0;

			for (int i = 0; i < items.size(); ++i) {
				CooperBillItemBean arg34 = (CooperBillItemBean) items.get(i);
				long bbItemId = arg34.getCooperBillItem().getBbItemId();
				itemName = "";
				if (!arg34.getStatus().equals("DELETE")) {
					BigDecimal arg35;
					int arg36;
					int arg37;
					CooperBillItemBean arg40;
					if (arg34.getCooperBillItem().getPriceType()
							.equals("AMOUNT")) {
						++arg33;
						itemName = "按数量计价的第" + arg33 + "个项目";
						arg35 = arg34.getCooperBillItem().getAmount();

						for (arg36 = i + 1; arg36 < items.size(); ++arg36) {
							arg40 = (CooperBillItemBean) items.get(arg36);
							if (!arg40.getStatus().equals("DELETE")
									&& arg40.getCooperBillItem().getPriceType()
											.equals("AMOUNT")) {
								arg37 = arg33 + 1;
								if (arg40.getCooperBillItem().getBbItemId() == bbItemId) {
									itemName = itemName + "+第" + arg37 + "个项目";
									arg35 = arg35.add(arg40.getCooperBillItem()
											.getAmount());
								}
							}
						}

						itemName = itemName + "的数量 " + arg35 + " ";
					} else if (arg34.getCooperBillItem().getPriceType()
							.equals("LENGTH")) {
						++arg32;
						itemName = "按长度计价的第" + arg32 + "个项目";
						arg35 = arg34.getCooperBillItem().getAmount();

						for (arg36 = i + 1; arg36 < items.size(); ++arg36) {
							arg40 = (CooperBillItemBean) items.get(arg36);
							if (!arg40.getStatus().equals("DELETE")
									&& arg40.getCooperBillItem().getPriceType()
											.equals("LENGTH")) {
								arg37 = arg32 + 1;
								if (arg40.getCooperBillItem().getBbItemId() == bbItemId) {
									itemName = itemName + "+第" + arg37 + "个项目";
									arg35 = arg35.add(arg40.getCooperBillItem()
											.getAmount());
								}
							}
						}

						itemName = itemName + "的数量 " + arg35 + " ";
					}

					if (arg34.getCooperBillItem().getPriceType().equals("AREA")) {
						++areaIndex;
						itemName = "按面积计价的第" + areaIndex + "个项目";
						arg35 = arg34.getCooperBillItem().getAmount();

						for (arg36 = i + 1; arg36 < items.size(); ++arg36) {
							arg40 = (CooperBillItemBean) items.get(arg36);
							if (!arg40.getStatus().equals("DELETE")
									&& arg40.getCooperBillItem().getPriceType()
											.equals("AREA")) {
								arg37 = areaIndex + 1;
								if (arg40.getCooperBillItem().getBbItemId() == bbItemId) {
									itemName = itemName + "+第" + arg37 + "个项目";
									arg35 = arg35.add(arg40.getCooperBillItem()
											.getAmount());
								}
							}
						}

						itemName = itemName + "的数量 " + arg35 + " ";
					}
				}

				if (bbItemId != 0L && !arg34.getStatus().equals("DELETE")) {
					BusinessBillItem arg38 = (BusinessBillItem) cooperBillItemDao
							.get(BusinessBillItem.class, bbItemId);
					BusinessBillItemExt arg39 = (BusinessBillItemExt) cooperBillItemDao
							.get(BusinessBillItemExt.class, bbItemId);
					if (arg38 != null
							&& arg39.getCoopAmou().compareTo(arg38.getAmount()) == 1) {
						throw new NotAllowException(itemName + "已超过业务单 "
								+ arg34.getCooperBillItem().getBbCode()
								+ " 的剩余外协数量 ");
					}
				}
			}
		}

	}

	private void updateBusinessProfit(List<?> items,
			BusinessBillDao businessBillDao,
			CooperBillItemDao cooperBillItemDao, IdGenerator idGenerator) {
		for (int i = 0; i < items.size(); ++i) {
			CooperBillItem item = (CooperBillItem) items.get(i);
			if (item.getBbCode() != null && !item.getBbCode().equals("")) {
				BusinessBill bb = businessBillDao.get(item.getBbCode());
				if (bb != null) {
					BusinessProfit p = businessBillDao.getBusinessProfit(item
							.getBbCode());
					if (p != null) {
						BusinessBillExt be = (BusinessBillExt) businessBillDao
								.get(BusinessBillExt.class, bb.getId());
						BigDecimal cooperCost = cooperBillItemDao.getCost(bb
								.getId());
						BigDecimal profitCost = bb.getCost()
								.add(be.getAdjustCost()).subtract(cooperCost)
								.subtract(p.getFeeCost())
								.subtract(p.getMaterialCost());
						p.setCooperCost(cooperCost);
						p.setProfitCost(profitCost);
						businessBillDao.update(p);
					}
				}
			}
		}

	}

	public void txExamine(long id, CooperBillDao cooperBillDao,
			PayBillDao payBillDao, AdjustBillDao adjustBillDao,
			long examinerId, String examinerName,
			BankAccountBalanceDao bankAccountBalanceDao,
			CooperBillItemDao cooperBillItemDao,
			BusinessBillDao businessBillDao, IdGenerator idGenerator)
			throws Exception {
		CooperBill b = (CooperBill) cooperBillDao.get(CooperBill.class, id);
		CooperBillExt be = (CooperBillExt) cooperBillDao.get(
				CooperBillExt.class, id);
		if (b != null && be.getEnabled().equals("Y")
				&& be.getExamined().equals("N")) {
			be.setPaiedCost(b.getImprest());
			if (b.getCost().subtract(be.getPaiedCost())
					.compareTo(new BigDecimal(0)) == 0) {
				be.setPaied("Y");
			}

			be.setExamined("Y");
			be.setExamineTime(new Date());
			be.setExaminerId(examinerId);
			be.setExaminerName(examinerName);
			cooperBillDao.update(be);
			String cooperatorName = "";
			Establishment est = (Establishment) bankAccountBalanceDao.get(
					Establishment.class, b.getCooperatorId());
			if (est != null) {
				cooperatorName = est.getEstName();
				if (b.getContent() != null && !b.getContent().equals("")) {
					cooperatorName = cooperatorName + "," + b.getContent();
				}
			}

			PageHelper.updateBankAccount(b.getBankAccountId(),
					b.getTallySetId(), b.getId(), b.getImprest(),
					b.getCooperDate(), be.getExamineTime(), b.getBillCode(),
					"COOPER_BILL", "", "外协预付", "P", b.getManagerName(),
					cooperatorName, "COOPER", "EXAM", false,
					(BankAccountDetailDao) null, bankAccountBalanceDao,
					idGenerator);
			if (est != null
					&& (est.getBusTime() == null || est.getBusTime().before(
							b.getCooperDate()))) {
				est.setBusTime(b.getCooperDate());
				cooperBillDao.update(est);
			}

			this.updateBusinessProfit(cooperBillItemDao.getList(b.getId()),
					businessBillDao, cooperBillItemDao, idGenerator);
			this.insertFinanceDetail(b, cooperBillDao, idGenerator);
		}

	}

	public void txUnExamine(long id, CooperBillDao cooperBillDao,
			BankAccountBalanceDao bankAccountBalanceDao,
			BankAccountDetailDao bankAccountDetailDao,
			CooperBillItemDao cooperBillItemDao,
			BusinessBillDao businessBillDao, IdGenerator idGenerator,
			FinanceDetailDao financeDetailDao, UserBean ub) throws Exception {
		CooperBill b = (CooperBill) cooperBillDao.get(CooperBill.class, id);
		CooperBillExt be = (CooperBillExt) cooperBillDao.get(
				CooperBillExt.class, id);
		if (b != null && be.getEnabled().equals("Y")
				&& be.getExamined().equals("Y")) {
			if (be.getHasPay().equals("Y")) {
				throw new NotAllowException("外协单" + b.getBillCode()
						+ "已经付款，不能反审核!");
			}

			if (be.getHasAdjust().equals("Y")) {
				throw new NotAllowException("外协单" + b.getBillCode()
						+ "已经调账，不能反审核!");
			}

			if (be.getHasGather().equals("Y")) {
				throw new NotAllowException("外协单" + b.getBillCode()
						+ "已经收货，不能反审核!");
			}

			be.setExamined("N");
			be.setAdjustCost(new BigDecimal(0));
			be.setPaied("N");
			be.setPayCost(new BigDecimal(0));
			be.setPaiedCost(new BigDecimal(0));
			b.setUpdateTime(new Date());
			be.setExaminerId(0L);
			be.setExaminerName("");
			cooperBillDao.update(be);
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			String mark = ub.getStaffer().getStafferName() + "于"
					+ sf.format(new Date()) + "反审核单据";
			PageHelper.updateBankAccount(b.getBankAccountId(),
					b.getTallySetId(), b.getId(),
					(new BigDecimal(0)).subtract(b.getImprest()),
					b.getCooperDate(), new Date(), b.getBillCode(),
					"COOPER_BILL", "", "外协预付", "P", b.getManagerName(), mark,
					"COOPER", "UN_EXAM", true, bankAccountDetailDao,
					bankAccountBalanceDao, idGenerator);
			this.updateBusinessProfit(cooperBillItemDao.getList(b.getId()),
					businessBillDao, cooperBillItemDao, idGenerator);
			financeDetailDao.delete(b.getId(), "COOPER_BILL");
		}

	}

	public String txCancel(long id, String reason, EnterpriseInfo ent,
			CooperBillItemDao cooperBillItemDao, UserBean ub,
			BankAccountBalanceDao bankAccountBalanceDao,
			BankAccountDetailDao bankAccountDetailDao,
			BusinessBillDao businessBillDao,
			BusinessBillItemDao businessBillItemDao, IdGenerator idGenerator,
			FinanceDetailDao financeDetailDao) throws Exception {
		String msg = "";
		CooperBill b = (CooperBill) cooperBillItemDao.get(CooperBill.class, id);
		CooperBillExt be = (CooperBillExt) cooperBillItemDao.get(
				CooperBillExt.class, id);
		if (b != null && be.getEnabled().equals("Y")) {
			if (be.getHasPay().equals("Y")) {
				msg = "外协单已经付款，不能作废!请点击“关联单据”，先作废外协单的关联单据，然后再作废外协单。";
			} else if (be.getHasAdjust().equals("Y")) {
				msg = "外协单已经调账，不能作废!请点击“关联单据”，先作废外协单的关联单据，然后再作废外协单。";
			} else if (be.getHasGather().equals("Y")) {
				msg = "外协单已经收货，不能作废!请点击“关联单据”，先作废外协单的关联单据，然后再作废外协单。";
			} else {
				be.setEnabled("N");
				be.setDisableManId(ub.getUser().getStafferId());
				cooperBillItemDao.update(be);
				List itemList = cooperBillItemDao.getList(id);
				if (be.getExamined().equals("Y")) {
					SimpleDateFormat log = new SimpleDateFormat(
							"yyyy-MM-dd HH:mm");
					String item = ub.getStaffer().getStafferName() + "于"
							+ log.format(new Date()) + "作废单据";
					PageHelper.updateBankAccount(b.getBankAccountId(),
							b.getTallySetId(), b.getId(),
							(new BigDecimal(0)).subtract(b.getImprest()),
							b.getCooperDate(), new Date(), b.getBillCode(),
							"COOPER_BILL", "", "外协预付", "P", b.getManagerName(),
							item, "COOPER", "CANC", true, bankAccountDetailDao,
							bankAccountBalanceDao, idGenerator);
					this.updateBusinessProfit(itemList, businessBillDao,
							cooperBillItemDao, idGenerator);
				}

				if (ent.getBusiCooComp() != null
						&& ent.getBusiCooComp().equals("Y")) {
					for (int arg24 = 0; arg24 < itemList.size(); ++arg24) {
						CooperBillItem arg26 = (CooperBillItem) itemList
								.get(arg24);
						if (arg26.getBbItemId() != 0L) {
							BusinessBillItem bbItem = (BusinessBillItem) cooperBillItemDao
									.get(BusinessBillItem.class,
											arg26.getBbItemId());
							BusinessBillItemExt bbIe = (BusinessBillItemExt) cooperBillItemDao
									.get(BusinessBillItemExt.class,
											arg26.getBbItemId());
							if (bbIe != null) {
								String oldCooperCompleted = bbIe.getCoopComp();
								BigDecimal cooperedAmount = cooperBillItemDao
										.getCooperedAmount(arg26.getBbItemId());
								bbIe.setCoopAmou(cooperedAmount);
								String newCooperCompleted;
								if (cooperedAmount
										.compareTo(bbItem.getAmount()) != -1) {
									newCooperCompleted = "Y";
								} else {
									newCooperCompleted = "N";
								}

								if (!newCooperCompleted
										.equals(oldCooperCompleted)) {
									bbIe.setCoopComp(newCooperCompleted);
									bbIe.setOperTime(new Date());
									bbIe.setOperId(ub.getUser().getStafferId());
									bbIe.setOperName(ub.getStaffer()
											.getStafferName());
									cooperBillItemDao.update(bbIe);
									BusinessBillExt bbe = (BusinessBillExt) cooperBillItemDao
											.get(BusinessBillExt.class,
													bbItem.getBillId());
									if (bbe != null) {
										if (businessBillItemDao
												.isAllCompleted(bbItem
														.getBillId())) {
											bbe.setCompleted("Y");
											bbe.setCompleteTime(new Date());
										} else {
											bbe.setCompleted("N");
											bbe.setCompleteTime((Date) null);
										}

										cooperBillItemDao.update(bbe);
									}
								}
							}
						}
					}
				}

				if (be.getExamined().equals("Y")) {
					financeDetailDao.delete(b.getId(), "COOPER_BILL");
				}

				SystemLog arg25 = new SystemLog();
				arg25.setId(idGenerator.getId(SystemLog.class.getName()));
				arg25.setActionCode("COOPER_BILL");
				arg25.setLogType("CANCEL_LOG");
				arg25.setCreaterId(ub.getStaffer().getId());
				arg25.setCreaterName(ub.getStaffer().getStafferName());
				arg25.setDepName(ub.getDepName());
				arg25.setDepId(ub.getStaffer().getDepartmentId());
				arg25.setStatus("S");
				arg25.setUserName(ub.getUser().getUserName());
				arg25.setIp(b.getBillCode());
				arg25.setMark("");
				arg25.setRelId(id);
				arg25.setCancelReason(reason);
				arg25.setTallySetId(b.getTallySetId());
				cooperBillItemDao.save(arg25);
				msg = "作废成功";
			}
		}

		return msg;
	}
}